home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / IR Man™ / PlayMovie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-16  |  10.3 KB  |  434 lines  |  [TEXT/KAHL]

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*        MCPlayMovie.                                                        */
  4. /*            by John Wang                                                    */
  5. /*                                                                            */
  6. /*        Description:    This program demonstrates playing movies with        */
  7. /*            QuickTime movie controllers.  It also allows the user to save    */
  8. /*            the movies in a flattened format using FlattenMovie().            */
  9. /*                                                                            */
  10. /*        Version:        1.0 Completed 11/17/91                                */
  11. /*                        1.1 Added palette support so that app restores        */
  12. /*                            color table when changed by another app.        */
  13. /*                                                                            */
  14. /*--------------------------------------------------------------------------*/
  15.  
  16. #include     <GestaltEqu.h>
  17. #include    <Palette.h>
  18. #include     "PlayMovie.h"
  19.  
  20.  
  21. /*------------------------------------------------------*/
  22. /*    Global Variables.                                    */
  23. /*------------------------------------------------------*/
  24.  
  25. extern short        quitFlag;
  26. MenuHandle            mymenu0, mymenu1;
  27. Boolean                playingMovie[MAXWINDOWS];
  28. WindowPtr            myWindow[MAXWINDOWS];
  29. Movie                myMovie[MAXWINDOWS];
  30. MovieController        mcPlay[MAXWINDOWS];
  31. int                    startlocation;
  32. CTabHandle            mycolors;
  33. PaletteHandle        srcPalette;
  34.  
  35. /*------------------------------------------------------*/
  36. /*    getMovieFromFile().                                    */
  37. /*------------------------------------------------------*/
  38.  
  39. Movie getMovieFromFile(void)
  40. {
  41.     OSErr                    err;
  42.     StandardFileReply        reply;
  43.     Point                    where = {200, 50};
  44.     SFTypeList                types;
  45.     short                    movieResRefNum;
  46.     short                    actualResId;
  47.     Movie                    theMovie;
  48.     
  49.     types[0] = 'MooV';
  50.     StandardGetFilePreview(nil, 1, types, &reply);
  51.     if (!reply.sfGood) return((Movie) 0);
  52.  
  53.     err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsRdPerm);
  54.     if (GetMoviesError()) return((Movie) 0);
  55.     if (err) return ((Movie) 0);
  56.     actualResId = DoTheRightThing;
  57.  
  58.     err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, 0L, newMovieActive, (Boolean *) 0);
  59.     if (GetMoviesError()) return((Movie) 0);
  60.     if (err) return ((Movie) 0);
  61.  
  62.     err = CloseMovieFile(movieResRefNum);
  63.     if (GetMoviesError()) return((Movie) 0);
  64.     if (err) return ((Movie) 0);
  65.  
  66.     return (theMovie);
  67. }
  68.  
  69. /*------------------------------------------------------*/
  70. /*    playMovie().                                            */
  71. /*------------------------------------------------------*/
  72.  
  73. OSErr playMovie(int    index)
  74. {
  75.     Rect            movieBounds;
  76.     Str255            windTitle;
  77.     
  78.     GetMovieBox(myMovie[index], &movieBounds);
  79.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  80. /*    movieBounds.right = movieBounds.right * 2;
  81.     movieBounds.bottom = movieBounds.bottom * 2; */
  82.     SetMovieBox(myMovie[index], &movieBounds);
  83.     OffsetRect(&movieBounds, startlocation, startlocation);
  84.  
  85.     
  86.     NumToString (index, windTitle);
  87.     myWindow[index] = NewCWindow(0L, &movieBounds, windTitle, 1, 0, (WindowPtr) -1, FALSE, 0L);
  88.     SetPalette ((WindowPtr) myWindow[index], srcPalette, TRUE);
  89.  
  90.     OffsetRect(&movieBounds, -startlocation, -startlocation);
  91.     startlocation += 40;
  92.     if (startlocation > 300) startlocation = 50;
  93.     
  94.     SetMovieGWorld(myMovie[index], (CGrafPtr) myWindow[index], 0);
  95.     if (GetMoviesError()) DebugStr("\PSetMovieGWorld error.");;
  96.     mcPlay[index] = NewMovieController(myMovie[index], &movieBounds, mcTopLeftMovie);
  97.     MCGetControllerBoundsRect(mcPlay[index], &movieBounds);
  98.     SizeWindow(myWindow[index], movieBounds.right - movieBounds.left,
  99.                             movieBounds.bottom - movieBounds.top, TRUE);
  100. /*    Uncomment these lines if you want to pre load the movie into ram.
  101.     Don't forget to increase the partition size when recompiling.
  102.     
  103.     GotoBeginningOfMovie(myMovie[index]);
  104.     if (LoadMovieIntoRam(myMovie[index], GetMovieTime(myMovie[index], 0L),
  105.                                     GetMovieDuration(myMovie[index]),
  106.                                     0) != noErr)
  107.         DebugStr("\PNot enough memory to load movie into ram.");
  108. */
  109. }
  110.  
  111.  
  112. /*------------------------------------------------------*/
  113. /*    init().                                                */
  114. /*------------------------------------------------------*/
  115.  
  116. void InitPlayMovie(void)
  117. {
  118.     OSErr                err;
  119.     int                    i;
  120.     long                QDfeature, OSfeature;
  121.  
  122.     /*    Set up menus.    */
  123.     mymenu0 = GetMenu(appleID);
  124.     AddResMenu(mymenu0, 'DRVR');
  125.     InsertMenu(mymenu0,0);
  126.     mymenu1 = GetMenu(fileID);
  127.     InsertMenu(mymenu1,0);
  128.     DrawMenuBar();
  129.     
  130.     /*    Set up variables.    */
  131.     startlocation = 50;
  132.     for (i = 0; i < MAXWINDOWS; i++) {
  133.         playingMovie[i] = FALSE;
  134.         myWindow[i] = nil;
  135.     }
  136.     mycolors = GetCTable (72);
  137.     if (mycolors == nil) Debugger();
  138.     srcPalette = NewPalette (((**mycolors).ctSize)+1, mycolors, pmTolerant, 0);
  139.  
  140.     /*    Use Gestalt to find if QuickDraw and QuickTime is available.    */
  141.     if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
  142.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  143.         if (err)
  144.             ExitToShell();
  145.         err = Gestalt(gestaltSystemVersion, &OSfeature);
  146.         if (err)
  147.             ExitToShell();
  148.         if (!quitFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0607)
  149.             ExitToShell();
  150.         err = Gestalt(gestaltQuickTime, &QDfeature);
  151.         if (err)
  152.             ExitToShell();
  153.     } else
  154.         ExitToShell();
  155.     
  156.     /*    Open QuickTime last.    */
  157.     err = EnterMovies();
  158.     if (err)
  159.         ExitToShell();
  160. }
  161.  
  162. /*------------------------------------------------------*/
  163. /*    ClosePlayMovie().                                            */
  164. /*------------------------------------------------------*/
  165.  
  166. void ClosePlayMovie(void)
  167. {
  168.     int i;
  169.  
  170.     for (i = 0; i < MAXWINDOWS; i++)
  171.         if (playingMovie[i] == TRUE) {
  172.             CloseComponent(mcPlay[i]);
  173.             DisposeMovie(myMovie[i]);
  174.             DisposeWindow(myWindow[i]);
  175.              playingMovie[i] = FALSE;
  176.         }
  177.     
  178.     ExitMovies();
  179.     ExitToShell();
  180. }
  181.  
  182. /*------------------------------------------------------*/
  183. /*    doOpenCommand().                                        */
  184. /*------------------------------------------------------*/
  185.  
  186. void doOpenCommand()
  187. {
  188.     int            i, useThisIndex;
  189.     
  190.     useThisIndex = -1;
  191.     
  192.     /*    Search for the first window that is nil.    */
  193.     for (i = MAXWINDOWS-1; i >= 0; i--)
  194.         if (myWindow[i] == nil)
  195.             useThisIndex = i;
  196.             
  197.     /*    If index = -1, then it means that there are no windows avaiable.    */
  198.     if (useThisIndex != -1) {
  199.         myMovie[useThisIndex] = getMovieFromFile();
  200.         if (myMovie[useThisIndex] != 0) {
  201.             playMovie(useThisIndex);
  202.             playingMovie[useThisIndex] = TRUE;
  203.         }
  204.     }
  205. }
  206.  
  207.  
  208. /*------------------------------------------------------*/
  209. /*    doCloseCommand().                                        */
  210. /*------------------------------------------------------*/
  211.  
  212. void doCloseCommand()
  213. {
  214.     int        i, useThisIndex;
  215.     WindowPtr    myTempWindow;
  216.  
  217.     /*    Close selected window.    */
  218.     myTempWindow = FrontWindow();
  219.     if (myTempWindow == nil) return;
  220.     for (i = 0; i < MAXWINDOWS; i++)
  221.         if (myWindow[i] == myTempWindow) {
  222.             CloseComponent(mcPlay[i]);
  223.             DisposeMovie(myMovie[i]);
  224.             DisposeWindow(myTempWindow);
  225.             playingMovie[i] = FALSE;
  226.             myWindow[i] = nil;
  227.         }
  228. }
  229.  
  230. /*------------------------------------------------------*/
  231. /*    doCommand().                                        */
  232. /*------------------------------------------------------*/
  233.  
  234. void doCommand(long    mResult)
  235. {
  236.     int                     theMenu, theItem;
  237.     char                    daName[256];
  238.     GrafPtr                 savePort;
  239.  
  240.     theItem = LoWord(mResult);
  241.     theMenu = HiWord(mResult);
  242.     
  243.     switch (theMenu) {
  244.         case appleID:
  245.             if (theItem == aboutMeCommand)
  246.                 ;
  247.             else {
  248.                 GetItem(mymenu0, theItem, daName);
  249.                 GetPort(&savePort);
  250.                 (void) OpenDeskAcc(daName);
  251.                 SetPort(savePort);
  252.             }
  253.             break;
  254.  
  255.         case fileID:
  256.             switch (theItem) {
  257.                 case openCommand:
  258.                     doOpenCommand();
  259.                     break;
  260.                 case closeCommand:
  261.                     doCloseCommand();
  262.                     break;
  263.                 case quitCommand:
  264.                     quitFlag = TRUE;
  265.                     break;
  266.                 default:
  267.                     break;
  268.                 }
  269.             break;
  270.     }
  271.     HiliteMenu(0);
  272.     return;
  273. }
  274.  
  275. /*------------------------------------------------------*/
  276. /*    playMovies().                                            */
  277. /*------------------------------------------------------*/
  278.  
  279. int playMovies(EventRecord *myEvent)
  280. {
  281.     int        i;
  282.     
  283.     for (i = 0; i < MAXWINDOWS; i++)
  284.         if (playingMovie[i] == TRUE) {
  285.             if (MCIsPlayerEvent(mcPlay[i], myEvent))
  286.                 return(TRUE);
  287.         }
  288.     return(FALSE);
  289. }
  290.  
  291. void
  292. BringToMooVFront(short frontBack) {
  293.     int        i;
  294.     int     cnt;
  295.     
  296.     for (i = 0; i < MAXWINDOWS; i++)
  297.         if (myWindow[i] == FrontWindow()) {
  298.             cnt = i;
  299.         }
  300.     if (frontBack) {
  301.         for (i = cnt+1; i < MAXWINDOWS; i++)
  302.             if (myWindow[i]) {
  303.                 SelectWindow(myWindow[i]);
  304.                 return;
  305.             }
  306.         for (i = 0; i < cnt; i++)
  307.             if (myWindow[i]) {
  308.                 SelectWindow(myWindow[i]);
  309.                 return;
  310.             }
  311.     }
  312.     else {
  313.         for (i = cnt-1; i >= 0; i--)
  314.             if (myWindow[i]) {
  315.                 SelectWindow(myWindow[i]);
  316.                 return;
  317.             }
  318.         for (i = MAXWINDOWS - 1; i > cnt; i--)
  319.             if (myWindow[i]) {
  320.                 SelectWindow(myWindow[i]);
  321.                 return;
  322.             }
  323.         
  324.     }
  325.  
  326. }
  327.  
  328. void IRMooVPlay(long Speed) {
  329.     short        i;
  330.     short        cnt = -1;
  331.     long        forward;
  332.     
  333.     for (i = 0; i < MAXWINDOWS; i++)
  334.         if (myWindow[i] == FrontWindow()) {
  335.             cnt = i;
  336.         }
  337.     if (cnt != -1) {
  338.         forward = Speed;
  339.         MCDoAction(mcPlay[cnt], mcActionPlay, (Ptr)forward);
  340.     }
  341. }
  342.  
  343. void IRMooVStep(long Speed) {
  344.     short        i;
  345.     short        cnt = -1;
  346.     long        forward;
  347.     
  348.     for (i = 0; i < MAXWINDOWS; i++)
  349.         if (myWindow[i] == FrontWindow()) {
  350.             cnt = i;
  351.         }
  352.     if (cnt != -1) {
  353.         forward = Speed;
  354.         MCDoAction(mcPlay[cnt], mcActionStep, (Ptr)forward);
  355.     }
  356. }
  357.  
  358. void IRMooVVolume(short Speed) {
  359.     short        i;
  360.     short        cnt = -1;
  361.     short        vol;
  362.     
  363.     for (i = 0; i < MAXWINDOWS; i++)
  364.         if (myWindow[i] == FrontWindow()) {
  365.             cnt = i;
  366.         }
  367.     if (cnt != -1) {
  368.         MCDoAction(mcPlay[cnt], mcActionGetVolume, &vol);
  369.         if (Speed)
  370.             vol = vol + 25;
  371.         else
  372.             vol = vol - 25;
  373.         if (vol < 0)
  374.             vol = 0;
  375.         if (vol > 255)
  376.             vol = 255;
  377.             
  378.         MCDoAction(mcPlay[cnt], mcActionSetVolume, (Ptr)vol);
  379.     }
  380. }
  381.  
  382.  
  383. void doAboutBox()
  384. {
  385.     int            i, useThisIndex;
  386.     
  387.     useThisIndex = -1;
  388.     
  389.     /*    Search for the first window that is nil.    */
  390.     for (i = MAXWINDOWS-1; i >= 0; i--)
  391.         if (myWindow[i] == nil)
  392.             useThisIndex = i;
  393.             
  394.     /*    If index = -1, then it means that there are no windows avaiable.    */
  395.     if (useThisIndex != -1) {
  396.         myMovie[useThisIndex] = getMovieFromFile();
  397.         if (myMovie[useThisIndex] != 0) {
  398.             playMovie(useThisIndex);
  399.             playingMovie[useThisIndex] = TRUE;
  400.         }
  401.     }
  402. }
  403.  
  404.  
  405. Movie LoadAboutBox(void)
  406. {
  407.     OSErr                    err;
  408.     StandardFileReply        reply;
  409.     Point                    where = {200, 50};
  410.     SFTypeList                types;
  411.     short                    movieResRefNum;
  412.     short                    actualResId;
  413.     Movie                    theMovie;
  414.     
  415.     reply.sfFile.vRefNum = 1;
  416.     reply.sfFile.parID = 0;
  417.     BlockMove("\pAbout Box Movie", reply.sfFile.name, 16);
  418.     
  419.     err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsRdPerm);
  420.     if (GetMoviesError()) return((Movie) 0);
  421.     if (err) return ((Movie) 0);
  422.     actualResId = DoTheRightThing;
  423.  
  424.     err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, 0L, newMovieActive, (Boolean *) 0);
  425.     if (GetMoviesError()) return((Movie) 0);
  426.     if (err) return ((Movie) 0);
  427.  
  428.     err = CloseMovieFile(movieResRefNum);
  429.     if (GetMoviesError()) return((Movie) 0);
  430.     if (err) return ((Movie) 0);
  431.  
  432.     return (theMovie);
  433. }
  434.